package cn.edu.xmu.restfuldemo.dao;


import cn.edu.xmu.restfuldemo.mapper.GoodsPoMapper;
import cn.edu.xmu.restfuldemo.mapper.ProductPoMapper;
import cn.edu.xmu.restfuldemo.model.BO.GoodsSku;
import cn.edu.xmu.restfuldemo.model.BO.Product;
import cn.edu.xmu.restfuldemo.model.PO.GoodsPo;
import cn.edu.xmu.restfuldemo.model.PO.GoodsSkuPo;
import cn.edu.xmu.restfuldemo.model.PO.ProductPo;
import cn.edu.xmu.restfuldemo.model.PO.ProductPoExample;
import cn.edu.xmu.restfuldemo.model.VO.GoodsRetVo;
import cn.edu.xmu.restfuldemo.model.VO.GoodsSkuVo;
import cn.edu.xmu.restfuldemo.model.VO.GoodsVo;
import cn.edu.xmu.restfuldemo.model.VO.ProductVo;
import cn.edu.xmu.restfuldemo.util.RedisUtil;
import cn.edu.xmu.restfuldemo.util.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Ming Qiu
 **/
@Repository
public class GoodsDao {
    @Autowired
    GoodsPoMapper goodsPoMapper;

    @Autowired
    ProductPoMapper productPoMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Value("${restfuldemo.goodsSku.expiretime}")
    private long goodsTimeout;

    /**
     * 不加redis的商品 SKU 完整信息
     * @param id
     * @return
     */
    public ReturnObject<GoodsRetVo>findGoodsRetVoByGoodsId(Long id){
        GoodsPo goodsPo = goodsPoMapper.selectByPrimaryKey(id);
        ProductPoExample example = new ProductPoExample();
        ProductPoExample.Criteria criteria  = example.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        criteria.andDisabledEqualTo((byte) 0);
        List<ProductPo>list = productPoMapper.selectByExample(example);
        GoodsVo goodsVo = new GoodsVo(goodsPo);
        List<Product>list1 = new ArrayList<>();
        for(ProductPo productPo:list){
            ProductVo productVo = new ProductVo(productPo);
            list1.add(productVo.newProduct());
        }
        GoodsRetVo goodsRetVo = new GoodsRetVo(goodsVo.newGoods(),list1);
        return new ReturnObject<>(goodsRetVo);
    }

    /**
     * 加redis的商品 SKU 完整信息
     * @param id
     * @return
     */
    public ReturnObject<GoodsRetVo>findGoodsRetVoByGoodsIdUsingRedis(Long id){
        List<GoodsRetVo>retVos = null;
        String key = "goods_product_"+id;
        GoodsRetVo goodsRetVo = (GoodsRetVo) redisUtil.get(key);
        if (null != goodsRetVo){
            return new ReturnObject<>(goodsRetVo);
        }
        GoodsPo goodsPo = goodsPoMapper.selectByPrimaryKey(id);
        ProductPoExample example = new ProductPoExample();
        ProductPoExample.Criteria criteria  = example.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        criteria.andDisabledEqualTo((byte) 0);
        List<ProductPo>list = productPoMapper.selectByExample(example);
        GoodsVo goodsVo = new GoodsVo(goodsPo);
        List<Product>list1 = new ArrayList<>();
        for(ProductPo productPo:list){
            ProductVo productVo = new ProductVo(productPo);
            list1.add(productVo.newProduct());
        }
        GoodsRetVo goodsRetVo1 = new GoodsRetVo(goodsVo.newGoods(),list1);
        redisUtil.set(key,  goodsRetVo1, goodsTimeout);
        return new ReturnObject<>(goodsRetVo1);
    }


}
